package fi.foyt.cs.api.items;

import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.ext.oauth.protectedresource.ScopedResource;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;

import fi.foyt.cs.Scope;
import fi.foyt.cs.api.APIResource;
import fi.foyt.cs.controller.ItemController;
import fi.foyt.cs.controller.SceneController;
import fi.foyt.cs.controller.UserController;
import fi.foyt.cs.persistence.domainmodel.items.Inventory;
import fi.foyt.cs.persistence.domainmodel.items.InventoryItem;
import fi.foyt.cs.persistence.domainmodel.items.Item;
import fi.foyt.cs.persistence.domainmodel.items.StashedItem;
import fi.foyt.cs.persistence.domainmodel.scene.Scene;
import fi.foyt.cs.persistence.domainmodel.user.User;

public class StashItemsResource extends APIResource implements ScopedResource {

  @Get
  @Post
  public Representation represent() throws ResourceException {
    Long inventoryItemId = getLongAttribute("inventoryItemId");
    Long sceneId = getLongAttribute("sceneId");
    Integer count = getIntegerAttribute("count");

    UserController userController = new UserController();
    ItemController itemController = new ItemController();
    SceneController sceneController = new SceneController();

    User user = userController.findUserByIdentifier(getClientInfo().getUser().getIdentifier());
    if (user != null) {
      InventoryItem inventoryItem = userController.findInventoryItemById(inventoryItemId);
      if (inventoryItem.getCount() >= count) {
        Inventory inventory = userController.findInventoryByUser(user);

        if (inventoryItem.getInventoryId() == inventory.getId().getId()) {
          Item item = itemController.findItemById(inventoryItem.getItemId());
          Scene scene = sceneController.findSceneById(sceneId);
          StashedItem stashedItem = itemController.findStashedItemByUserAndSceneAndItem(user, item, scene);
          if (stashedItem != null) {
            itemController.updateStashedItemCount(stashedItem, stashedItem.getCount() + count);

          } else {
            itemController.createStashedItem(user, item, scene, count);
          }

          // TODO: Does this work ?
          if (inventoryItem.getCount() == count) {
            userController.deleteInventoryItem(inventoryItem);
          } else {
            userController.updateInventoryItemCount(inventoryItem, inventoryItem.getCount() - count);
          }
        } else {
          throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN, "Item not owned by logged user");
        }

      } else {
        throw new ResourceException(Status.CLIENT_ERROR_FAILED_DEPENDENCY, "No enought items in inventory");
      }

    } else {
      throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED, "User not found");
    }

    return new EmptyRepresentation();
  }

  public String getOwner(Reference uri) {
    return null;
  }

  public String[] getScope(Reference uri) {
    return new String[] { Scope.StashedItems.getIntenal() };
  }
}
